#include "Buffer.h"


Buffer::Buffer()
{
	input = Queue<unsigned char>();
	output = Queue<unsigned char>();
}

Buffer::Buffer(Queue<unsigned char>& in, Queue<unsigned char>& out)
{
	input = in;
	output = out;
}

Buffer::Buffer(unsigned int inSize, unsigned outSize)
{
	input = Queue<unsigned char>(1, inSize);
	output = Queue<unsigned char>(1, outSize);
}

unsigned char Buffer::getInput()
{
	return input.pop();
}
bool Buffer::inputEmpty()
{
	if(input.empty()==1)
		return true;
	else 	
		return false;
}

bool Buffer::outputFull()
{
	if(output.full()==1)
		return true;
	else 
		return false;
}

void Buffer::setOutput(unsigned char d)
{
	//if(output.full() != 1)
		output.push(d);
}

unsigned char Buffer::readByte()
{
	unsigned char t = 0;
	if(output.empty()!=1)
		t = output.pop();
	return t;
}

void* Buffer::read(unsigned int length)
{
	unsigned char* charData = (unsigned char*) malloc(length);
	
	unsigned int i = 0;
	for(i = 0; i < length; i++)
	{
		if(output.empty() != 1)
			charData[i] = output.pop();
		else
			charData[i] = 0x00;
	}
	void* data = (void*) charData;
	return data;
}

void Buffer::writeByte(unsigned char byte)
{
	if(input.full() != 1)
		input.push(byte);
}

void Buffer::write(void* data, unsigned int length)
{
	unsigned char* charData = (unsigned char*) data;
	unsigned int i = 0;
	for(i = 0; i < length; i++)
	{
		if(input.full() != 1)
			input.push(charData[i]);
	}
}

bool Buffer::full()
{
	//never full
	if(input.full() == 1)
		return true;
	else	
		return false;
}

bool Buffer::empty()
{
	if(output.empty()==1)
		return true;
	else
		return false;

}
